//
//  YKEssenceViewController.m
//  baisi
//
//  Created by 吴高烽 on 16/8/29.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKEssenceViewController.h"
#import "YKRecommendTagsController.h"
#import "YKPushGuideView.h"
#import <SVProgressHUD.h>
#import "YKTopicsController.h"
@interface YKEssenceViewController ()<UIScrollViewDelegate>
@property (nonatomic ,weak)UIView *indicatorView;
@property (nonatomic ,weak)UIButton *currentBtn;
@property (nonatomic ,weak)UIScrollView *contentView;
@property (nonatomic ,weak)UIView *titleView;
@end

@implementation YKEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationView];
    [self setupChildren];
    [self setupTitleView];

    [self setupContentView];
    [YKPushGuideView show];

//    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];

}
-(void)setupNavigationView{
//    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" selectImage:@"MainTagSubIconClick" target:self action:@selector(menBtnClick)];
}
-(void)setupChildren{
    YKTopicsController *word = [[YKTopicsController alloc]init];
    word.title = @"段子";
    word.type = YKTopicTypeWord ;
    [self addChildViewController:word];

    YKTopicsController *video =[[YKTopicsController alloc]init];
    video.title = @"视频";
    video.type = YKTopicTypeVideo ;
    [self addChildViewController:video];
    
    YKTopicsController *online = [[YKTopicsController alloc]init];
    online.title = @"网红";
    online.type = YKTopicTypeOnlie ;
    [self addChildViewController:online];
    
    YKTopicsController *all = [[YKTopicsController alloc]init];
    all.title = @"全部";
    all.type = YKTopicTypeAll ;
    [self addChildViewController:all];

    YKTopicsController *picture = [[YKTopicsController alloc]init];
    picture.title = @"图片";
    picture.type = YKTopicTypePicture ;
    [self addChildViewController:picture ];
   
    YKTopicsController *beauty = [[YKTopicsController alloc]init];
    beauty.title = @"美女";
    beauty.type = YKTopicTypeBeauty ;
    [self addChildViewController:beauty];
    
}
-(void)setupContentView{
    // 告诉系统不要自动调整inset（ScrollView及其子类的inser）
    self.automaticallyAdjustsScrollViewInsets = NO ;
    UIScrollView *contentView =[[UIScrollView alloc]init];
    contentView.delegate = self ;
    contentView.y = 99;
    contentView.pagingEnabled = YES ;
    contentView.frame = self.view.bounds ;
//    contentView.width = self.view.width ;
//    contentView.height = self.view.height - 99;
//    contentView.backgroundColor = [UIColor grayColor];
//    [contentView addSubview:[UIButton buttonWithType:UIButtonTypeContactAdd]];
//    UISwitch *s = [[UISwitch alloc]init];
//    s.y = 740 ;
//    [contentView addSubview:s];
//    contentView.contentSize =CGSizeMake(0, 800);
//    [self.view addSubview:contentView ];
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
    [self.view insertSubview:contentView atIndex:0];
    self.contentView = contentView ;
    [self scrollViewDidEndScrollingAnimation:contentView];
}
/** 设置顶部标签栏 */
-(void)setupTitleView{
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.width, 35)];
    titleView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.7];
//    titleView.width = self.view.width;
//    titleView.height = 35;
//    titleView.y = 64 ;
    [self.view addSubview:titleView];
    // 底部的红色指示器
    UIView *indicatorView = [[UIView alloc]init];
    indicatorView.backgroundColor = [UIColor redColor];
    indicatorView.height = 2 ;
    indicatorView.y = titleView.height -indicatorView.height ;
    self.indicatorView = indicatorView ;
    
//  NSArray *titles =@[@"全部",@"视频",@"图片",@"段子",@"网红",@"美女"];
    NSArray *titles = self.childViewControllers ;
    NSInteger count = titles.count ;
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnHeight = titleView.height ;
    CGFloat btnWidth = titleView.width/count ;
    
    for (NSInteger i = 0; i<count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i ;
        btnX = i*btnWidth ;
        btn.frame = CGRectMake(btnX, btnY, btnWidth, btnHeight);
        UIViewController *vc = titles[i] ;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        [btn addTarget:self action:@selector(titleEvent:) forControlEvents:UIControlEventTouchUpInside];
//        [btn setTitle:titles[i] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [titleView addSubview:btn];
        if (i==0) {
            btn.enabled = NO;
            [btn.titleLabel sizeToFit];
            self.indicatorView.width = btn.titleLabel.width;
            self.indicatorView.centerX = btn.centerX ;
            self.currentBtn = btn ;
        }
    }
    [titleView addSubview:indicatorView];
    self.titleView = titleView ;
}
-(void)titleEvent:(UIButton*)button{
    // 修改按钮状态
    self.currentBtn.enabled = YES;
    button.enabled = NO;
    self.currentBtn = button ;
//    self.currentBtn.selected = NO ;
//    button.selected =! button.selected ;
//    self.currentBtn = button ;
    // 动画
[UIView animateWithDuration:0.25 animations:^{
    self.indicatorView.width = button.titleLabel.width;
    self.indicatorView.centerX = button.centerX ;
}];
    // 滚动
    CGPoint offset = self.contentView.contentOffset ;
    offset.x = button.tag * self.contentView.width ;
    [self.contentView setContentOffset:offset animated:YES];
}
-(void)menBtnClick{
    YKLog(@"精华");
    YKRecommendTagsController *recomvc = [[YKRecommendTagsController alloc]init];
    [self.navigationController pushViewController:recomvc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UIScrollViewDelegate
// scrollView 减速运行
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger index = scrollView.contentOffset.x / scrollView.width ;
    [self titleEvent:self.titleView.subviews[index]];
}
// scrollView停止滚动
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

    NSInteger  index = scrollView.contentOffset.x / scrollView.width ;
    UITableViewController *tableVC = self.childViewControllers[index];
    tableVC.view.x = scrollView.contentOffset.x ;
    tableVC.view.y = 0 ;
    tableVC.view.height = scrollView.height ;
//    通用性查，如果不是UITableViewController，而是UIViewController
//    CGFloat bottom = self.tabBarController.tabBar.height ;
//    CGFloat top = CGRectGetMaxY(self.titleView.frame);
//    tableVC.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
//    tableVC.tableView.scrollIndicatorInsets = tableVC.tableView.contentInset ;
    [scrollView addSubview:tableVC.view];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
